﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Configuration;
using System.Collections.Generic;
using System.Management.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;

namespace EntLibContrib.Query.Configuration.Manageability.Queries
{
	/// <summary>
	/// Represents the configuration information for an instance of
	/// <see cref="EntLibContrib.Query.Configuration.DataQueryData"/>.
	/// </summary>
	/// <seealso cref="EntLibContrib.Query.Configuration.ServiceQueryData"/>
	/// <seealso cref="EntLibContrib.Query.Configuration.QueryData"/>
	/// <seealso cref="EntLibContrib.Query.Configuration.QueryDataBase"/>
	/// <seealso cref="NamedConfigurationSetting"/>
	/// <seealso cref="ConfigurationSetting"/>
	[ManagementEntity]
	public class DataQuerySetting : QuerySetting
	{
		#region Fields
		private string databaseInstanceName;
		private string command;
		private string commandType;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the name of the database instance.
		/// </summary>
		/// <value>The name of the database instance.</value>
		[ManagementConfiguration]
		public string DatabaseInstanceName
		{
			get { return databaseInstanceName; }
			set { databaseInstanceName = value; }
		}

		/// <summary>
		/// Gets or sets the command.
		/// </summary>
		/// <value>The command.</value>
		[ManagementConfiguration]
		public string Command
		{
			get { return command; }
			set { command = value; }
		}

		/// <summary>
		/// Gets or sets the type of the command.
		/// </summary>
		/// <value>The type of the command.</value>
		[ManagementConfiguration]
		public string CommandType
		{
			get { return commandType; }
			set { commandType = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="DataQuerySetting"/> class.
		/// </summary>
		/// <param name="sourceElement">The source element.</param>
		/// <param name="name">The name.</param>
		/// <param name="customCommandName">Name of the custom command.</param>
		/// <param name="parameterSetName">Name of the parameter set.</param>
		/// <param name="databaseInstanceName">Name of the database instance.</param>
		/// <param name="command">The command.</param>
		/// <param name="commandType">Type of the command.</param>
		public DataQuerySetting(DataQueryData sourceElement,
														string name, 
														string customCommandName,
														string parameterSetName,
														string databaseInstanceName,
														string command,
														string commandType)
			: base(sourceElement, name, customCommandName, parameterSetName)
		{
			this.databaseInstanceName = databaseInstanceName;
			this.command = command;
			this.commandType = commandType;
		}
		#endregion

		#region Public Static Methods
		/// <summary>
		/// Returns the <see cref="DataQuerySetting"/> instance corresponding to the provided values for the key properties.
		/// </summary>
		/// <param name="applicationName">Name of the application.</param>
		/// <param name="sectionName">Name of the section.</param>
		/// <param name="name">The command name.</param>
		/// <returns>
		/// The published <see cref="DataQuerySetting"/> instance specified by the values for the key properties,
		/// or <see langword="null"/> if no such an instance is currently published.
		/// </returns>
		[ManagementBind]
		public static DataQuerySetting BindInstance(string applicationName,
																							 string sectionName,
																							 string name)
		{
			return BindInstance<DataQuerySetting>(applicationName, sectionName, name);
		}

		/// <summary>
		/// Returns an enumeration of the published <see cref="DataQuerySetting"/> instances.
		/// </summary>
		/// <returns>Enumerable list of <see cref="DataQuerySetting"/> instances</returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate"), ManagementEnumerator]
		public static IEnumerable<DataQuerySetting> GetInstances()
		{
			return GetInstances<DataQuerySetting>();
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Saves the changes on the <see cref="DataQuerySetting"/> to its corresponding configuration object.
		/// </summary>
		/// <param name="sourceElement">The configuration object on which the changes must be saved.</param>
		/// <returns>
		/// 	<see langword="true"/> if changes have been saved, <see langword="false"/> otherwise.
		/// </returns>
		/// <remarks>Must be overridden by subclasses to perform the actual save.</remarks>
		protected override bool SaveChanges(ConfigurationElement sourceElement)
		{
			return DataQueryDataWmiMapper.SaveChanges(this, sourceElement);
		}
		#endregion
	}
}
